/**
 * author wen
 * cunyu
 * email wjd_13@hotmail.com
 */
package com.cunyu.action;

import com.cunyu.bean.Muser;
import com.cunyu.service.OrderPoolService;
import com.cunyu.service.TagService;
import com.cunyu.util.ExcelReader;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 从文件等导入数据的功能 ！
 * @author win
 * 2019/05/06-13:31
 */
@Controller
public class ImportDataController extends BaseController{

    @Resource
    OrderPoolService orderPoolService ;

    @Resource
    TagService tagService ;

    @RequestMapping("/import/pool")
    public void importOrderPool(HttpServletRequest request,
                                HttpServletResponse response){
        Muser user = getOnlineUser();

        MultipartResolver resolver = new CommonsMultipartResolver(request.getSession()
                .getServletContext());
        MultipartHttpServletRequest multipartRequest = resolver.resolveMultipart(request);
        MultipartFile excel = multipartRequest.getFile("file");

        Map<String, Map<String, Object>> tagNameMap = tagService.getTagNameMap();
        List<Object[]> orderPools = ExcelReader.orderPoolFile(excel,tagNameMap,orderPoolService);

        int va = orderPoolService.importData(orderPools,user) ;

        printOK(response,"success",va);
    }

}
